package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocBackRecordAccountAddDTO;
import org.jsola.hr.dto.EmpSocBackRecordAccountUpdateDTO;
import org.jsola.hr.query.EmpSocBackRecordAccountQuery;
import org.jsola.hr.service.IEmpSocBackRecordAccountService;
import org.jsola.hr.vo.EmpSocBackRecordAccountListVO;
import org.jsola.hr.vo.EmpSocBackRecordAccountVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 社保回款账户
 *
 * @author zhr
 */
@RestController("hrEmpSocBackRecordAccountController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocBackRecordAccounts")
public class EmpSocBackRecordAccountController implements IEmpSocBackRecordAccountControllerAPI {

    @Autowired
    private IEmpSocBackRecordAccountService empSocBackRecordAccountService;

    @Override
    @PostMapping
    public Result<EmpSocBackRecordAccountVO> save(@Valid @RequestBody EmpSocBackRecordAccountAddDTO empSocBackRecordAccountAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保回款账户权限校验

        return Result.success(
                empSocBackRecordAccountService.save(empSocBackRecordAccountAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocBackRecordAccountUpdateDTO empSocBackRecordAccountUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保回款账户权限校验

        int count = empSocBackRecordAccountService.update(empSocBackRecordAccountUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empSocBackRecordAccountId}")
    public Result<Integer> delete(@PathVariable String empSocBackRecordAccountId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保回款账户权限校验

        int count = empSocBackRecordAccountService.deleteByIds(tokenUser, empSocBackRecordAccountId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocBackRecordAccountIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保回款账户权限校验

        int count = empSocBackRecordAccountService.deleteByIds(tokenUser, empSocBackRecordAccountIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocBackRecordAccountId}")
    public Result<EmpSocBackRecordAccountVO> findById(@PathVariable String empSocBackRecordAccountId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款账户权限校验

        return Result.success(
            empSocBackRecordAccountService.selectById(empSocBackRecordAccountId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocBackRecordAccountListVO>> listAll(@Valid EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保回款账户权限校验

        return Result.success(
            empSocBackRecordAccountService.select(empSocBackRecordAccountQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocBackRecordAccountListVO>> page(@Valid EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empSocBackRecordAccountService.selectPage(empSocBackRecordAccountQuery, tokenUser.getSiteId())
        );
    }


}

